package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsOrderService;
import com.qf.util.ResponseUtil;
import com.qf.vo.ShipChannelListVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Cc
 * @version 1.0
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Autowired
    private DtsOrderService dtsOrderService;


    /**
     * 接口地址 : http://localhost:8083/admin/order/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissions("admin:order:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "订单列表")
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "20") Integer limit,
                       @RequestParam(value = "sort", defaultValue = "add_time") String sort,
                       @RequestParam(value = "order", defaultValue = "desc") String order) {


        IPage<DtsOrder> dtsCategoryIPage = dtsOrderService.page(page, limit, sort, order);
        Map<String, Object> map = new HashMap<>();
        map.put("total", dtsCategoryIPage.getTotal());
        map.put("items", dtsCategoryIPage.getRecords());
        return ResponseUtil.ok(map);

    }

    /**
     * - 接口地址 : http://localhost:8083/admin/order/listShipChannel
     */
    @RequiresPermissions("admin:order:listShipChannel")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "快递列表")
    @GetMapping("/listShipChannel")
    public Object listShipChannel() {

        List<ShipChannelListVo> listFind = dtsOrderService.listShipChannel();

        ShipChannelListVo listVo = new ShipChannelListVo();

        HashMap map = new HashMap();

        for (ShipChannelListVo vo : listFind) {
            String value = vo.getValue();
            String label = vo.getLabel();
            listVo.setValue(value);
            listVo.setLabel(label);
        }


        map.put("shipChannelList", listVo);
        return ResponseUtil.ok(map);

    }

    /**
     * 接口地址 : http://localhost:8083/admin/order/detail?id=98
     */
    @RequiresPermissions("admin:order:detail")
    @RequiresPermissionsDesc(menu = {"商场管理", "订单管理"}, button = "订单详情")
    @GetMapping("/detail")
    public Object detail( Integer id) {
        DtsOrder Order = dtsOrderService.orderById(id);
        DtsUser dtsUser = dtsOrderService.userByid(Order.getUserId());
        List<DtsOrderGoods> dtsOrderGoods = dtsOrderService.goosById(Order.getId());
        HashMap map = new HashMap();
        map.put("order", Order);
        map.put("user", dtsUser);
        map.put("orderGoods", dtsOrderGoods);


        return ResponseUtil.ok(map);
    }
}
